#include "libui/ui.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <process.h>
#include <windows.h>
//

uiBox* mainPage;
uiBox* buttonPage;
uiArea* mainArea;
uiButton *button1;
uiButton *button2;

int onClosing(uiWindow *w, void *data);
void onButton1_Clicked(uiButton *b, void *data);
void onButton2_Clicked(uiButton *b, void *data);

void handlerDraw(uiAreaHandler* ah, uiArea* area, uiAreaDrawParams * p);
void handlerMouseEvent(uiAreaHandler* ah, uiArea* area, uiAreaMouseEvent * e);
void handlerMouseCrossed(uiAreaHandler* ah, uiArea* area, int left);
void handlerDragBroken(uiAreaHandler* ah, uiArea* area);
int  handlerKeyEvent(uiAreaHandler* ah, uiArea* area, uiAreaKeyEvent * e);


int main(int argc, char const *argv[])
{
    uiAreaHandler areaHandler;
    areaHandler.Draw = handlerDraw;
	areaHandler.MouseEvent = handlerMouseEvent;
	areaHandler.MouseCrossed = handlerMouseCrossed;
	areaHandler.DragBroken = handlerDragBroken;
	areaHandler.KeyEvent = handlerKeyEvent;
    
    ///
    uiInitOptions o;
    const char* err = uiInit(&o);
    if (err != NULL) 
    {
		fprintf(stderr, "error initializing ui: %s\n", err);
		uiFreeInitError(err);
		return 1;
	}
    uiWindow* w = uiNewWindow("MyGUI Draw Program", 400, 320, 0);
    uiWindowOnClosing(w, onClosing, NULL);
    ///////////////
    mainPage = uiNewVerticalBox();
    mainArea = uiNewArea(&areaHandler);
    uiBoxAppend(mainPage, uiControl(mainArea), 1);

    buttonPage = uiNewHorizontalBox();
    uiBoxAppend(mainPage, uiControl(buttonPage), 0);
    uiWindowSetChild(w, uiControl(mainPage));
	uiControlShow(uiControl(w));
	uiMain();
    return 0;
}

int onClosing(uiWindow *w, void *data)
{
	uiQuit();
	return 1;
}


void handlerDraw(uiAreaHandler* ah, uiArea* area, uiAreaDrawParams * p)
{
    uiDrawBrush brush;
    brush.Type = uiDrawBrushTypeSolid;

    brush.R = 255;
    brush.G = 255;
    brush.B = 255;
    brush.A = 1.0;

    uiDrawPath *path = uiDrawNewPath(uiDrawFillModeWinding);
    uiDrawPathAddRectangle(path, 0, 0, p->AreaWidth, p->AreaHeight);

    uiDrawPathEnd(path);   
    uiDrawFill(p->Context, path, &brush);
    uiDrawFreePath(path);
    
    brush.R = 0;
    brush.G = 0;
    brush.B = 0;
    
    uiDrawStrokeParams sp;
    memset(&sp, 0, sizeof (uiDrawStrokeParams));
    sp.Dashes = NULL;
	sp.NumDashes = 0;
	sp.DashPhase = 0;
    sp.Cap = uiDrawLineCapRound;
	sp.Join = uiDrawLineJoinMiter;
	sp.Thickness = 2;
	sp.MiterLimit = uiDrawDefaultMiterLimit;

    double MAPHEIGHT = p->AreaHeight;
    double MAPWIDTH = p->AreaWidth - 50;
    double cx, cy, lx, ly;

    path = uiDrawNewPath(uiDrawFillModeWinding);
    uiDrawPathNewFigure(path, 0, 0);
    uiDrawPathNewFigureWithArc(path, 0, 0, 275, 1.5 * uiPi, 2 * uiPi, 0);
    uiDrawPathEnd(path);
    uiDrawStroke(p->Context, path, &brush, &sp);
    uiDrawFreePath(path);

    cx = 275;
    cy = 0;
    for (size_t i = 0; i < 20; i++)
    {
        path = uiDrawNewPath(uiDrawFillModeWinding);
        uiDrawPathNewFigure(path, 0, 0);
        uiDrawPathNewFigureWithArc(path, cx, cy, 20, 0, 2 * uiPi, 0);
        uiDrawPathEnd(path);
        uiDrawStroke(p->Context, path, &brush, &sp);
        uiDrawFreePath(path);
        cx = 275 * sin( 3.0 / 4.0 * uiPi * i / 20);
        cy = 275 * cos( 3.0 / 4.0 * uiPi * i / 20);
    }  
}
void handlerMouseEvent(uiAreaHandler* ah, uiArea* area, uiAreaMouseEvent * e)
{

}
void handlerMouseCrossed(uiAreaHandler* ah, uiArea* area, int left)
{

}
void handlerDragBroken(uiAreaHandler* ah, uiArea* area)
{

}
int  handlerKeyEvent(uiAreaHandler* ah, uiArea* area, uiAreaKeyEvent * e)
{  
    return 0;
}
